package cn.it.xiaohan.note.dao;

import cn.hutool.core.util.StrUtil;
import cn.it.xiaohan.note.po.NoteType;
import cn.it.xiaohan.note.util.DBUtil;

import javax.servlet.http.Part;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class NoteTypeDao {


    public List<NoteType> findTypeByUserId(Integer userId) {
        // 定义Sql语句
        String sql = "select * from tb_note_type where userId = ?";
        // 设置参数集合
        List<Object> params = new ArrayList<>();
        params.add(userId);
        List<NoteType> typeList = BaseDao.queryRows(sql,params,NoteType.class);
        // 返回类型列表集合
        return typeList;
    }

    public long findNoteByTypeId(String typeId) {
        // 定义sql语句
        String sql = "select count(*) from tb_note where typeId = ?";
        // 设置参数
        List<Object> params = new ArrayList<>();
        params.add(typeId);
        long num = (long) BaseDao.findSingleValue(sql, params);
        return num;
    }

    public int deleteTypeById(String typeId) {
        String sql = "delete from tb_note_type where typeId = ?";
        List<Object> params = new ArrayList<>();
        params.add(typeId);
        int row = BaseDao.executeUpdate(sql, params);
        return row;
    }

    /**
     * 验证类型名称唯一性
     * @param typeName
     * @param userId
     * @param typeId
     * @return
     */
    public Integer checkTypeName(String typeName, Integer userId, String typeId) {
        // 定义sql语句
        String sql = "select * from tb_note_type where userId = ? and typeName = ?";
        // 设置参数
        List<Object> params = new ArrayList<>();
        params.add(userId);
        params.add(typeName);
        NoteType noteType = (NoteType) BaseDao.queryRow(sql,params,NoteType.class);
        if (noteType == null){
            return 1;
        } else {
            if (typeId.equals(noteType.getTypeId().toString())){
                return 1;
            }
        }
        return 0;
    }

    /**
     * 添加操作
     * @param typeName
     * @param userId
     * @return
     */
    public Integer addType(String typeName, Integer userId) {
        Integer key = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try{
            // 获取数据库连接
            connection = DBUtil.getConnection();
            // 定义Sql语句
            String sql = "insert into tb_note_type (typeName,userId) values(?,?)";
            // 预编译
            preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            // 设置参数
            preparedStatement.setString(1,typeName);
            preparedStatement.setInt(2,userId);
            int row = preparedStatement.executeUpdate();
            if (row > 0){
                resultSet = preparedStatement.getGeneratedKeys();
                if (resultSet.next()){
                    key = resultSet.getInt(1);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return key;
    }

    public Integer updateType(String typeName, String typeId) {
        // 定义sql语句
        String sql = "update tb_note_type set typeName = ? where typeId = ?";
        // 设置参数
        List<Object> params = new ArrayList<>();
        params.add(typeName);
        params.add(typeId);
        int row = BaseDao.executeUpdate(sql,params);
        return row;
    }
}
